var prefix = ctx + "system/user"
var type=document.getElementById("type_hidden").value; 

$(document).ready(function(){
	$('body').layout({ west__size: 185 });
	
	if(type !=null && type!=''){
		queryDissionUserList();
		//查询离职员工信息
		//$('#btnExpand').show();
	}else{
		//查询在职员工信息
		queryUserList();
		//queryDeptTreeDaTa();
		//$('#btnCollapse').show();
	}
	
	
});

//在职员工信息
function queryUserList() {
	var columns = [{
            checkbox: true
        },
        {
            field: 'userId',
            title: '用户ID',
            align: 'center'
        },
        {
            field: 'userName',
            title: '用户名称',
            align: 'center'
        },
        {
            field: 'sex',
            title: '性别',
            align: 'center',
            formatter: function(value, row, index) {
                if (value == '0') {
                    return '<span class="label label-success">男</span>';
                } else if (value == '1') {
                    return '<span class="label label-danger">女</span>';
                }
            }
        },
        {
            field: 'phonenumber',
            title: '手机号码',
            align: 'center'
        },
        {
            field: 'dept.deptName',
            title: '所属组织',
            align: 'center'
        },
        {
            field: 'accessLogin',
            title: '登录状态',
            align: 'center',
            formatter: function(value, row, index) {
                if (value == '0') {
                    return '<span class="label label-success">正常登录</span>';
                } else if (value == '1') {
                    return '<span class="label label-danger">禁止登录</span>';
                }
            }
        },
        {
            title: '操作',
            align: 'center',
            formatter: function(value, row, index) {
            	if(row.userType == "N") {
            		var actions = [];
                	actions.push('<a class="btn btn-primary btn-sm ' + editFlag + '" href="#" title="编辑" onclick="edit(\'' + row.userId + '\')"><i class="fa fa-edit"></i></a> ');
                	actions.push('<a class="btn btn-warning btn-sm ' + removeFlag + '" href="#" title="删除" onclick="remove(\'' + row.userId + '\')"><i class="fa fa-remove"></i></a> ');
                	actions.push('<a class="btn btn-success btn-sm ' + resetPwdFlag + '"  href="#" title="重置" onclick="resetPwd(\'' + row.userId + '\')"><i class="fa fa-key"></i></a>');
                	return actions.join('');
				} else {
					return "";
				}
            }
        }];
	var url = prefix + "/list?type="+type;
	$.initTable(columns, url);
}


//离职员工信息
function queryDissionUserList() {
	var columns = [{
            checkbox: true
        },
        {
            field: 'userId',
            title: '用户ID',
            align: 'center'
        },
        {
            field: 'userName',
            title: '用户名称',
            align: 'center'
        },
        {
            field: 'sex',
            title: '性别',
            align: 'center',
            formatter: function(value, row, index) {
                if (value == '0') {
                    return '<span class="label label-success">男</span>';
                } else if (value == '1') {
                    return '<span class="label label-danger">女</span>';
                }
            }
        },
        {
            field: 'phonenumber',
            title: '手机号码',
            align: 'center'
        },
        {
            field: 'dept.deptName',
            title: '所属部门',
            align: 'center'
        },
        {
            field: 'dimissionTime',
            title: '离职时间',
            align: 'center',
        },
        {
            title: '操作',
            align: 'center',
            formatter: function(value, row, index) {
            	if(row.userType == "N") {
            		var actions = [];
                	actions.push('<a class="btn btn-primary btn-sm ' + editFlag + '" href="#" title="编辑" onclick="edit(\'' + row.userId + '\')"><i class="fa fa-edit"></i></a> ');
                	actions.push('<a class="btn btn-warning btn-sm ' + removeFlag + '" href="#" title="删除" onclick="remove(\'' + row.userId + '\')"><i class="fa fa-remove"></i></a> ');
                	return actions.join('');
				} else {
					return "";
				}
            }
        }];
	var url = prefix + "/list?type="+type;
	$.initTable(columns, url);
}


function queryDeptTreeDaTa()
{
	alert("queryDeptTreeDaTa");
	// 树结构初始化加载
	var setting = {view:{selectedMulti:false},data:{key:{title:"title"},simpleData:{enable:true}},
		callback:{onClick:function(event, treeId, treeNode){
			tree.expandNode(treeNode);
			var opt = { query : { deptId : treeNode.id, parentId : treeNode.pId, } };
			$('.bootstrap-table').bootstrapTable('refresh', opt);
		}}
	}, tree, loadTree = function(){
		$.get(ctx + "system/dept/treeData", function(data) {
		    tree = $.fn.zTree.init($("#tree"), setting, data); //.expandAll(true);
		    // 展开第一级节点
		    var nodes = tree.getNodesByParam("level", 0);
		    for (var i = 0; i < nodes.length; i++) {
		        tree.expandNode(nodes[i], true, false, false);
		    }
		    // 展开第二级节点
		    nodes = tree.getNodesByParam("level", 1);
		    for (var i = 0; i < nodes.length; i++) {
		        tree.expandNode(nodes[i], true, false, false);
		    }
		}, null, null, "正在加载，请稍后...");
	};loadTree();
	
	$('#btnExpand').click(function() {
		alert("btnExpand");
		tree.expandAll(true);
	    $(this).hide();
	    $('#btnCollapse').show();
	});
	$('#btnCollapse').click(function() {
		alert("btnCollapse");
		tree.expandAll(false);
	    $(this).hide();
	    $('#btnExpand').show();
	});
	$('#btnRefresh').click(function() {
		alert("btnRefresh");
	    loadTree();
	});
}

/*用户管理-部门*/
function dept() {
	var url = ctx + "system/dept";
	createMenuItem(url, "部门管理");
}

/*用户管理-删除*/
function remove(userId) {
	$.modalConfirm("确定要删除选中用户吗？", function() {
		_ajax(prefix + "/remove/" + userId, "", "post");
    })
}

/*用户管理-修改*/
function edit(userId) {
    var url = prefix + '/edit/' + userId;
    layer_showAuto("修改用户", url);
}

/*用户管理-新增*/
function add() {
    var url = prefix + '/add';
    layer_showAuto("新增用户", url);
}

/*用户管理-重置密码*/
function resetPwd(userId) {
    var url = prefix + '/resetPwd/' + userId;
    layer_show("重置密码", url, '800', '300');
}

// 批量强退
function batchRemove() {
	var rows = $.getSelections("userId");
	if (rows.length == 0) {
		$.modalMsg("请选择要删除的数据", "warning");
		return;
	}
	$.modalConfirm("确认要删除选中的" + rows.length + "条数据吗?", function() {
		_ajax(prefix + '/batchRemove', { "ids": rows }, "post");
	});
}
